package com.atguigu.gmall.activity.task;

import com.atguigu.gmall.activity.mapper.SeckillMapper;
import com.atguigu.gmall.activity.util.DateUtil;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 秒杀商品存入缓存的定时任务
 * @author 搭忆长安
 * @version 1.0
 * @date 2021年07月15日 10:13
 * @ClassName:SeckillTask
 * @Description: TODO
 */
@Component
public class SeckillTask {

    @Autowired
    private SeckillMapper seckillMapper;


    @Autowired
    private RedisTemplate  redisTemplate;
    /**
     * 秒杀商品存入redis缓存的定时任务
     */
    @Scheduled(cron = "0/15 * * * * *")
    public  void  seckillInttoRedisTask(){
        //计算时间段:5个时间段
        List<Date> dateMenus = DateUtil.getDateMenus();
        for (Date dateMenu : dateMenus) {
            //从数据库查询出对应时间段的商品数据2021-7-14 14:00
            String startTime = DateUtil.data2str(dateMenu, DateUtil.PATTERN_YYYY_MM_DDHHMM);
            //结算时间段的截止时间: 2021-7-14 16:00
            Date date = DateUtil.addDateHour(dateMenu, 2);
            String endTime = DateUtil.data2str(date, DateUtil.PATTERN_YYYY_MM_DDHHMM);
            //拼接条件
            LambdaQueryWrapper<SeckillGoods> wrapper = new LambdaQueryWrapper<>();
            //审核状态必须是通过的
            wrapper.eq(SeckillGoods::getStatus, "1");
            //剩余库存大于0
            wrapper.gt(SeckillGoods::getStockCount, 0);
            //  startTime=<开始时间 结束时间<endTime
            wrapper.ge(SeckillGoods::getStartTime, startTime);
            wrapper.le(SeckillGoods::getEndTime, endTime);
            // redis中没有这个数据的
            String key = DateUtil.data2str(dateMenu, DateUtil.PATTERN_YYYYMMDDHH);
            Set keys = redisTemplate.boundHashOps("SeckillGoods_" + key).keys();
            if(keys != null && keys.size() > 0){
                wrapper.notIn(SeckillGoods::getId, keys);
            }
            //查询数据库的数据
            List<SeckillGoods> seckillGoods = seckillMapper.selectList(wrapper);
            //存入Redis: SeckillGoods_2021071414
            for (SeckillGoods seckillGood : seckillGoods) {
                redisTemplate.boundHashOps("SeckillGoods_" + key).put(seckillGood.getId(), seckillGood);
                //获取商品的库存数量
                Integer stockCount = seckillGood.getStockCount();
                //构建一个数量长度的数组
                String[] seckillGoodsQueue = getSeckillGoodsQueue(seckillGood.getId(), stockCount);
                //存入redis中
                redisTemplate.boundListOps("SeckillGoodsQueue_"+seckillGood.getId()).leftPushAll(seckillGoodsQueue);
                //库存进行记录
                redisTemplate.boundHashOps("SeckillGoodsStockNum_").increment(seckillGood.getId(),stockCount);

            }
        }
    }

    /**
     * 构建商品库存个数长度的数组
     * @param goodsId
     * @param stockCount
     * @return
     */
    private  String[] getSeckillGoodsQueue(String goodsId,Integer stockCount){
        //构建一个库存长度的数组
        String[] ids = new String[stockCount];
        for (int i = 0 ;i<stockCount;i++) {
            ids[i]=goodsId;
        }
        //返回数据
        return  ids;
    }
}
